package com.aqie.medium.backtrack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 39 组合总和
 * todo
 */
public class CombinationSum {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(candidates);
        backtrack(candidates, target, res, 0, new ArrayList<Integer>());
        return res;
    }

    private void backtrack(int[] candidates, int target, List<List<Integer>> res, int i, ArrayList<Integer> integers) {
        if (target < 0) return;
        if (target == 0){
            res.add(new ArrayList<>(integers));
            return;
        }
        for (int start = i; start < candidates.length; start++){
            if (target < candidates[start]) break;
            integers.add(candidates[start]);
            backtrack(candidates,target - candidates[start], res, start, integers);
            integers.remove(integers.size() - 1);
        }
    }


    public static void main(String[] args) {
        int[] arr = {2,3,6,7};
        int target = 7;
    }
}
